/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.controller;

import com.tarena.lbs.attach.enums.FileTypeEnum;

import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.vo.FileVO;
import com.tarena.lbs.attach.service.AttachService;
import com.tarena.lbs.base.protocol.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

//@Api(value = "attach", tags = "附件中心管理")
@Slf4j
@Validated
@RestController
@RequestMapping("attach")
@Tag(name = "附件服务", description = "附件中心管理")
public class AttachController {
    @Autowired
    private AttachService attachService;

    @Value("${image_path}")
    private String IMAGE_PATH;

    @Value("${url}")
    private String URL;

    @PostMapping("upload")
    //@ApiOperation(value = "上传附件")
    @Operation(summary = "上传附件")
    public Result<FileVO> uploadAttachInfo(@RequestPart("file") MultipartFile file) throws Exception {
        FileVO fileVO = new FileVO();
        AttachPO attachPO = file2PO(file);
        BeanUtils.copyProperties(attachPO, fileVO);
        int i = attachService.insertAttach(attachPO);
        fileVO.setId(i);
        fileVO.setUrl(URL + attachPO.getFileUuid());

        return new Result<>(fileVO);
    }


    private AttachPO file2PO(MultipartFile file) {

        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            String suffixName = null;
            if (originalFilename != null) {
                suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            UUID uuid = UUID.randomUUID();
            String realName = uuid + suffixName;
            log.debug("realName : {}", realName);
            String realPath = IMAGE_PATH + realName;
            log.debug("realPath : {}", realPath);
            File folder = new File(IMAGE_PATH);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            try {
                file.transferTo(new File(realPath));
                AttachPO attachPO = new AttachPO();
                attachPO.setFileUuid(realName);
                attachPO.setClientFileName(originalFilename);
                attachPO.setContentLength((int) file.getSize());
                attachPO.setContentType(String.valueOf(FileTypeEnum.PICTURE));
                return attachPO;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}
